<template>
    <div>
        <el-table :data="tableData"
                  v-loading="isLoading"
                  style="width: 100%;margin: 10px auto 20px;"
                  row-key="id"
                  border
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
            <el-table-column prop="title"
                             label="菜单名称"
                             width="150">
            </el-table-column>
            <el-table-column prop="href"
                             label="地址">
            </el-table-column>
            <el-table-column fixed="right"
                             label="操作"
                             width="200">
                <template slot-scope="{row}">
                    <el-button @click="addChild(row)"
                               type="text"
                               size="small"
                               v-if="row.hasChild">
                        新增子项
                    </el-button>
                    <el-button @click="deleteMenu(row)"
                               type="text"
                               size="small">
                        删除
                    </el-button>
                    <el-button @click="editMenu(row)"
                               type="text"
                               size="small">
                        编辑
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog :title="dialogTitle"
                   :visible.sync="isVisible">
            <edit-menu ref="formRef" />
        </el-dialog>

    </div>
</template>

<script>

import EditMenu from '../views/menu/EditMenu'

export default {
    data() {
        return {
            isLoading: false,
            tableData: [],
            isVisible: false,
            dialogTitle: '',
            isAddVisible: false,
            isEditVisible: false
        }
    },
    mounted() {
        this.initMenuList()
    },
    methods: {
        addChild(row) {
            this.isVisible = true
            this.dialogTitle = '新增'
            this.$nextTick(() => {
                this.$refs.formRef.initData(row,'child')
            })
        },
        addMenu() {
            this.isVisible = true
            this.dialogTitle = '新增'
            this.$nextTick(() => {
                this.$refs.formRef.initData()
            })
        },
        editMenu(row) {
            this.isVisible = true
            this.dialogTitle = '编辑'
            this.$nextTick(() => {
                this.$refs.formRef.initData(row)
            })
        },
        deleteMenu(row) {
            this.$service.deleteMenu(row.id)
                .then(res => {
                    this.initMenuList()
                })
        },

        initMenuList() {
            this.isLoading = true
            this.$service.getmenu().then(res => {
                //此处做个数据处理  数据结构有问题
                this.isLoading = false
                this.tableData = res.data

            })
        },
    },
    components: {
        EditMenu,
    }
}
</script>

<style>
</style>